﻿using ProductMonitor.Command;
using ProductMonitor.Models;
using ProductMonitor.MyUserControl;
using ProductMonitor.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;

namespace ProductMonitor.ViewModels
{
    public class MainWindowVM : ObservableObject
    {
        private UserControl _MonitorUC;

        public MainWindowVM()
        {
            EnviromentList = new ObservableCollection<EnviromentModel>();
            EnviromentList.Add(new EnviromentModel { EnItemName = "光照(Lux)",EnItemValue=123 });
            EnviromentList.Add(new EnviromentModel { EnItemName = "噪音(db)",EnItemValue=123 });
            EnviromentList.Add(new EnviromentModel { EnItemName = "温度(℃)",EnItemValue=123 });
            EnviromentList.Add(new EnviromentModel { EnItemName = "湿度(%)",EnItemValue=123 });
            EnviromentList.Add(new EnviromentModel { EnItemName = "PM2.5(m3)",EnItemValue=123 });
            EnviromentList.Add(new EnviromentModel { EnItemName = "硫化氢(PPM)",EnItemValue=123 });
            EnviromentList.Add(new EnviromentModel { EnItemName = "氮气(PPM)",EnItemValue=123 });

            AlarmList = new ObservableCollection<AlarmModel>();
            AlarmList.Add(new AlarmModel { Num = "01", Msg = "设备温度过高", Time = "2023-11-23 18:34:56", Duration = 7 });
            AlarmList.Add(new AlarmModel { Num = "01", Msg = "设备压力过高", Time = "2023-11-23 18:34:56", Duration = 10 });
            AlarmList.Add(new AlarmModel { Num = "01", Msg = "设备转速过快", Time = "2023-11-23 18:34:56", Duration = 12 });
            AlarmList.Add(new AlarmModel { Num = "01", Msg = "设备欠压", Time = "2023-11-23 18:34:56", Duration = 17 });

            DeviceList = new ObservableCollection<DeviceModel>();
            DeviceList.Add(new DeviceModel { DeviceItem = "电能(Kw.h)", Value = 60.8 });
            DeviceList.Add(new DeviceModel { DeviceItem = "电压(V)", Value = 60.8 });
            DeviceList.Add(new DeviceModel { DeviceItem = "电流(A)", Value = 60.8 });
            DeviceList.Add(new DeviceModel { DeviceItem = "压差(Kpa)", Value = 60.8 });
            DeviceList.Add(new DeviceModel { DeviceItem = "温度(℃)", Value = 60.8 });
            DeviceList.Add(new DeviceModel { DeviceItem = "转速(r/min)", Value = 60.8 });

            RaderList = new ObservableCollection<RaderModel>();
            RaderList.Add(new RaderModel { ItemName = "排风烟机", Value = 90 });
            RaderList.Add(new RaderModel { ItemName = "客梯", Value = 30.00 });
            RaderList.Add(new RaderModel { ItemName = "供水机", Value = 34.34 });
            RaderList.Add(new RaderModel { ItemName = "喷淋管", Value = 56.35 });
            RaderList.Add(new RaderModel { ItemName = "变压器", Value = 20 });

            StuffOutWorkList = new List<StuffOutWorkModel>();
            StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "小赵", Position = "技术员", OutWorkCount = 103 });
            StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "小钱", Position = "技术员", OutWorkCount = 23 });
            StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "小孙", Position = "技术员", OutWorkCount = 12 });
            StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "小李", Position = "技术员", OutWorkCount = 13 });
            StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "张三丰", Position = "技术员", OutWorkCount = 24 });

            WorkShopList = new List<WorkShopModel>();
            WorkShopList.Add(new WorkShopModel { WorkShopName = "贴片车间", StopCount = 1 , WaitCount = 8, WorkingCount = 32, WrongCount = 4});
            WorkShopList.Add(new WorkShopModel { WorkShopName = "封装车间", StopCount = 1 , WaitCount = 8, WorkingCount = 32, WrongCount = 4});
            WorkShopList.Add(new WorkShopModel { WorkShopName = "焊接车间", StopCount = 1 , WaitCount = 8, WorkingCount = 32, WrongCount = 4});
            WorkShopList.Add(new WorkShopModel { WorkShopName = "组装车间", StopCount = 1 , WaitCount = 8, WorkingCount = 32, WrongCount = 4});

            #region 初始化机台信息

            MachineList = new List<MachineModel>();

            //随机生成数据
            Random random = new Random();
            for (int i = 0; i < 20; i++)
            {
                int plan = random.Next(100, 1000);//计划机台数量
                int finshed = random.Next(100, plan);//实际完成数量

                int statusNum = random.Next(0, 2);//必须到2，因为不包含右区间

                MachineList.Add(new MachineModel
                {
                    Name = "焊接机-" + (i + 1),
                    Status = statusNum == 1 ? "作业中" : "停止中",
                    StatusColor = (statusNum == 1 ? "LightGreen" : "Red"),
                    PlanCount = plan,
                    FinshedCount = finshed,
                    OrderNO = "H20250105" + i,
                });
            }
            #endregion
        }
        public UserControl MonitorUC
		{
			get {
                if(_MonitorUC == null)
                {
                    _MonitorUC = new MonitorUC();
                }
                return _MonitorUC; }
			set { _MonitorUC = value;
                RaisePropertyChanged();
            }
		}
        #region 打开车间命令
        private RelayCommand _showDetailCmd;
        public RelayCommand ShowDetailCmd
        {
            get
            {
                if (_showDetailCmd == null)
                {
                    _showDetailCmd = new RelayCommand(ShowDetailUC);
                }
                return _showDetailCmd;
            }
        }
       
        public void ShowDetailUC()
        {
            WorkShopDetailUC workShopDetailUC = new WorkShopDetailUC();
            this.MonitorUC = workShopDetailUC;
            ThicknessAnimation thicknessAnimation = new ThicknessAnimation(
                new Thickness(0, 50, 0, -50),  // 起始值：Margin(左, 上, 右, 下)
                new Thickness(0, 0, 0, 0),      // 结束值：最终的 Margin
                new TimeSpan(0, 0, 0, 0, 800)  // 动画时长：4000 毫秒（4秒）
            );
            DoubleAnimation doubleAnimation = new DoubleAnimation(
                0,                              // 起始值：透明度 0（完全透明）
                1,                              // 结束值：透明度 1（完全不透明）
                new TimeSpan(0, 0, 0, 0, 800)  // 动画时长：4000 毫秒（4秒）
            );
            // 指定动画作用的目标控件是 workShopDetailUC
            Storyboard.SetTarget(thicknessAnimation, workShopDetailUC);
            Storyboard.SetTarget(doubleAnimation, workShopDetailUC);
            // 位移动画作用于控件的 Margin 属性
            Storyboard.SetTargetProperty(thicknessAnimation, new PropertyPath("Margin"));
            // 透明度动画作用于控件的 Opacity 属性
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity"));
            // 创建故事板（动画容器）
            Storyboard storyboard = new Storyboard();
            // 将两个动画添加到故事板中
            storyboard.Children.Add(thicknessAnimation);
            storyboard.Children.Add(doubleAnimation);
            // 开始执行所有动画（两个动画将同时启动）
            storyboard.Begin();

        }
        #endregion

        #region 返回监控命令
        private RelayCommand _goBackCmd;
        public RelayCommand GoBackCmd
        {
            get
            {
                if (_goBackCmd == null)
                {
                    _goBackCmd = new RelayCommand(GoBack);
                }
                return _goBackCmd;
            }
        }

        private void GoBack()
        {
            this.MonitorUC = new MonitorUC();

        }
        #endregion

        #region 展示列细节
        private RelayCommand _showColumnDetailCmd;
        public RelayCommand ShowColumnDetailCmd
        {
            get
            {
                if (_showColumnDetailCmd == null)
                {
                    _showColumnDetailCmd = new RelayCommand(ShowColumnDetail);
                }
                return _showColumnDetailCmd;
            }
        }
        private void ShowColumnDetail()
        {
            WorkShopDetailUC wshop = this.MonitorUC as WorkShopDetailUC;
            if (wshop != null)
            {
                wshop.detail.Visibility = Visibility.Visible;
                //设置动画(Animation)效果(由下而上)
                ThicknessAnimation thicknessAnimation = new ThicknessAnimation(new Thickness(0, 650, 0, 0),
                                                            new Thickness(0, 0, 0, 0), new TimeSpan(0, 0, 0, 0, 6400));
                //设置透明度
                DoubleAnimation doubleAnimation = new DoubleAnimation(0, 1, new TimeSpan(0, 0, 0, 0, 400));

                //添加属性到窗体之上
                Storyboard.SetTarget(thicknessAnimation, wshop.detailContent);
                Storyboard.SetTarget(doubleAnimation, wshop.detailContent);

                //指定动画效果作用的具体属性
                Storyboard.SetTargetProperty(thicknessAnimation, new PropertyPath("Margin"));
                Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity"));

                //实例化
                Storyboard storyboard = new Storyboard();

                //将我们设置的动画都加入到“脚本”（集合了一系列的动画）中
                storyboard.Children.Add(thicknessAnimation);
                storyboard.Children.Add(doubleAnimation);
                storyboard.Begin();
            }

        }
        #endregion
        #region 关闭列细节
        private RelayCommand _colseColumnDetailCmd;
        public RelayCommand ColseColumnDetailCmd
        {
            get 
            {
                if (_colseColumnDetailCmd == null)
                {
                    _colseColumnDetailCmd = new RelayCommand(ColseColumnDetail);
                }
                return _colseColumnDetailCmd;
            }
            set { _colseColumnDetailCmd = value; }
        }

        private void ColseColumnDetail()
        {
            WorkShopDetailUC wshop = this.MonitorUC as WorkShopDetailUC;
            if (wshop != null)
            {
                //设置动画(Animation)效果(由下而上)
                ThicknessAnimation thicknessAnimation = new ThicknessAnimation(new Thickness(0, 0, 0, 0),
                                                        new Thickness(0, 300, 0, -300), new TimeSpan(0, 0, 0, 0, 400));
                //设置透明度
                DoubleAnimation doubleAnimation = new DoubleAnimation(0, 1, new TimeSpan(0, 0, 0, 0, 400));

                //添加属性到窗体之上
                Storyboard.SetTarget(thicknessAnimation, wshop.detailContent);
                Storyboard.SetTarget(doubleAnimation, wshop.detailContent);

                //指定动画效果作用的具体属性
                Storyboard.SetTargetProperty(thicknessAnimation, new PropertyPath("Margin"));
                Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity"));

                //实例化
                Storyboard storyboard = new Storyboard();

                //将我们设置的动画都加入到“脚本”（集合了一系列的动画）中
                storyboard.Children.Add(thicknessAnimation);
                storyboard.Children.Add(doubleAnimation);

                //动画完成事件订阅
                storyboard.Completed += (se, ev) =>
                {
                    wshop.detail.Visibility = Visibility.Collapsed;
                };
                //不能忘记加
                storyboard.Begin();
                
            }
        }
        #endregion

        private string _TimeStr = DateTime.Now.ToString("HH:mm:ss");
        public string TimeStr
        {
            get { return _TimeStr; }
            set
            {
                _TimeStr = value;
                RaisePropertyChanged();
            }
        }

        private string _MounthDay = DateTime.Now.ToString("yyyy-MM-dd");

        public string MounthDay
        {
            get { return _MounthDay; }
            set { _MounthDay = value;
                RaisePropertyChanged();
            }
        }

        private int _WeekDay = DateTime.Now.DayOfWeek > 0 ? Convert.ToInt16(DateTime.Now.DayOfWeek): Convert.ToInt16(DateTime.Now.DayOfWeek + 7);

        public string WeekDay
        {
            get {
                switch (_WeekDay)
                {
                    case 1:
                        return "星期一";
                    case 2:
                        return "星期二";
                    case 3:
                        return "星期三";
                    case 4:
                        return "星期四";
                    case 5:
                        return "星期五";
                    case 6:
                        return "星期六";
                    default:
                        return "星期七";
                }
             }
            set { _WeekDay = Convert.ToInt16(value);
                RaisePropertyChanged();
            }
        }

        private string _MachineCount="0298";

        public string MachineCount
        {
            get { return _MachineCount; }
            set { _MachineCount = value;
                RaisePropertyChanged();
            }
        }

        private string _ProductCount = "0629";

        public string ProductCount
        {
            get { return _ProductCount; }
            set { _ProductCount = value;
                RaisePropertyChanged();
            }
        }

        private string  _BadCount= "0629";

        public string BadCount
        {
            get { return _BadCount; }
            set
            {
                _BadCount = value;
                RaisePropertyChanged();
            }
        }

        #region 环境监控
        private ObservableCollection<EnviromentModel> _EnviromentList;

        public ObservableCollection<EnviromentModel> EnviromentList
        {
            get { return _EnviromentList; }
            set { _EnviromentList = value;
                RaisePropertyChanged() ;
            }
        }

        #endregion

        private ObservableCollection<AlarmModel> _AlarmList;

        public ObservableCollection<AlarmModel> AlarmList
        {
            get { return _AlarmList; }
            set { _AlarmList = value;RaisePropertyChanged(); }
        }

        //设备列表
        private ObservableCollection<DeviceModel> _DeviceList;

        public ObservableCollection<DeviceModel> DeviceList
        {
            get { return _DeviceList; }
            set { _DeviceList = value;RaisePropertyChanged(); }
        }

        private ObservableCollection<RaderModel> _RaderList;

        public ObservableCollection<RaderModel> RaderList
        {
            get { return _RaderList; }
            set { _RaderList = value; }
        }

        private List<StuffOutWorkModel> _StuffOutWorkList;

        public List<StuffOutWorkModel> StuffOutWorkList
        {
            get { return _StuffOutWorkList; }
            set { _StuffOutWorkList = value;RaisePropertyChanged(); }
        }

        private List<WorkShopModel> _WorkShopList;

        public List<WorkShopModel> WorkShopList
        {
            get { return _WorkShopList; }
            set { _WorkShopList = value; RaisePropertyChanged(); }
        }

        private List<MachineModel> _MachineList;

        public List<MachineModel> MachineList
        {
            get { return _MachineList; }
            set { _MachineList = value; RaisePropertyChanged(); }
        }

    }
}
